import * as cc from "cc";
import OnlineImage from "../components/sprite/OnlineImage";
import NodeUtils from "./NodeUtils";


export default class SpriteUtils {

    /**只增不减进度条动画 */
    static progressUpTo(progressbar: cc.ProgressBar, progress: number, duration: number = 0.1): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            let oldProgress = progressbar.progress
            if (oldProgress >= progress) {
                resolve()
                return;
            }
            if (progress > 1) progress = 1
            if (progress < 0) progress = 0

            cc.tween(progressbar).stop();
            cc.tween(progressbar)
                .to(duration, { progress: progress })
                .call(() => {
                    resolve()
                }).start();
        })
    }
    /**进度条动画 */
    static progressTo(progressbar: cc.ProgressBar, progress: number, duration: number = 0.1): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            let oldProgress = progressbar.progress

            if (progress > 1) progress = 1
            if (progress < 0) progress = 0

            cc.tween(progressbar).stop();
            cc.tween(progressbar)
                .to(duration, { progress: progress })
                .call(() => {
                    resolve()
                }).start();
        })
    }
    /**进度条动画 */
    static spriteFiledUpTo(sprite: cc.Sprite, progress: number, duration: number = 0.1): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            let oldProgress = sprite.fillRange;
            if (oldProgress >= progress) {
                resolve()
                return;
            }

            if (progress > 1) progress = 1
            if (progress < 0) progress = 0

            cc.tween(sprite).stop();
            cc.tween(sprite)
                .to(duration, { fillRange: progress })
                .call(() => {
                    resolve()
                }).start();
        })
    }
    /**进度条动画 */
    static spriteFiledTo(sprite: cc.Sprite, progress: number, duration: number = 0.1): Promise<void> {
        return new Promise<void>((resolve, reject) => {
            let oldProgress = sprite.fillRange;

            if (progress > 1) progress = 1
            if (progress < 0) progress = 0

            cc.tween(sprite).stop();
            cc.tween(sprite)
                .to(duration, { fillRange: progress })
                .call(() => {
                    resolve()
                }).start();
        })
    }


    //动态加载图片的方法
    static loadOnlineImg(container: cc.Node | cc.Sprite, url: string, type?: string): Promise<any> {
        return new Promise<any>((resolve, reject) => {
            let nd = (container instanceof cc.Component) ? container.node : container
            let onlineImg: OnlineImage = NodeUtils.getOrAddComponent<OnlineImage>(nd, OnlineImage);
            if (onlineImg) {
                onlineImg.loadImg(url, type, (succeed: boolean) => {
                    if (succeed) {
                        // Log.log("===loadOnlineImg succeed")
                        resolve(true);
                    } else {
                        // Log.error("===图片加载失败")
                        resolve(false);
                    }
                })
            }
        })
    }

}